package com.example.anquan.controller;

import com.example.anquan.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class IndexController {
    @GetMapping("/{type}/{path}")
    public String toDetail(@PathVariable("type")String type,
                           @PathVariable("path")String path){
        return "/"+type+"/"+path;
    }
    @GetMapping("/login")
    public String toLoginPage(){
        return "/login";
    }
    @GetMapping("/index")
    public String toIndexPage(){
        return "/index";
    }
    @GetMapping("/logout")
    public String toLogoutPage(){
        return "/logout";
    }
    @GetMapping("/register")
    public String register() {
        return "/register";
    }

    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public String registerUser( @RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        if (userService.register(username, password)) {
            return "redirect:/login";
        } else {
            model.addAttribute("msg", "该用户已注册！");
            return "/register";
        }
    }
}
